package com.sphd.Restaurant.adapters;

import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sphd.Restaurant.R;
import com.sphd.Restaurant.base.MyApplication;
import com.sphd.Restaurant.infos.Dishes;
import com.sphd.Restaurant.infos.RestaurantInfo;
import com.sphd.Restaurant.interfFace.TrueMoneyCallBack;
import com.sphd.Restaurant.model.Model;


import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Hu on 2016/10/11.
 */

public class DiesAdapter extends BaseQuickAdapter<Dishes> {
    private TrueMoneyCallBack addMoneyCallBack;//点击+的监听
    private TrueMoneyCallBack removeMoneyCallBack;//点击-的监听
    private RestaurantInfo restaurantInfo;
    private int disNo = 0;


    public DiesAdapter(List<Dishes> data, TrueMoneyCallBack addMoneyCallBack, TrueMoneyCallBack removeMoneyCallBack, RestaurantInfo restaurantInfo) {
        super(R.layout.order_item_item, data);
        this.addMoneyCallBack = addMoneyCallBack;
        this.removeMoneyCallBack = removeMoneyCallBack;
        this.restaurantInfo = restaurantInfo;
    }

    @Override
    protected void convert(BaseViewHolder helper, Dishes item) {

        helper.setText(R.id.tv_item_name, item.getDishes_name())
                .setText(R.id.tv_item_money, turn(item.getDishes_price(),1)+"")
                .setText(R.id.tv_item_money_vip, turn(item.getDishes_vipprice(),1) + "");
        ImageView imageView = helper.getView(R.id.iv_item_img);
        ImageView itemAdd = helper.getView(R.id.iv_item_add);
        ImageView itemSub = helper.getView(R.id.iv_item_subtract);
        TextView tvNo = helper.getView(R.id.tv_item_number);
        disNo = item.getOrder_no();
        if (disNo <= 0) {
            itemSub.setVisibility(View.INVISIBLE);
            tvNo.setVisibility(View.INVISIBLE);

        } else {
            itemSub.setVisibility(View.VISIBLE);
            tvNo.setVisibility(View.VISIBLE);
            tvNo.setText(disNo+"");
        }
        setImage(imageView, item.getDishes_pic());
        itemAdd.setOnClickListener(view -> {
            disNo = item.getOrder_no();
            if (restaurantInfo.getCg_inventory() == 1) {
                if (disNo == item.getDishes_inventory()) {
                    Snackbar.make(view, "选购数量已超库存", Snackbar.LENGTH_SHORT).show();
                } else {
                    disNo++;
                    item.setOrder_no(disNo);
                    addMoneyCallBack.sendcallback(item.getDishes_price());
                    itemSub .setVisibility(View.VISIBLE);
                    tvNo.setVisibility(View.VISIBLE);
                    tvNo.setText(disNo+"");
                }
            }else {
                disNo++;
                item.setOrder_no(disNo);
                addMoneyCallBack.sendcallback(item.getDishes_price());
                itemSub .setVisibility(View.VISIBLE);
                tvNo.setVisibility(View.VISIBLE);
                tvNo.setText(disNo+"");
            }

        });
        itemSub.setOnClickListener(view -> {
            disNo = item.getOrder_no();
            disNo--;
            item.setOrder_no(disNo);
            removeMoneyCallBack.sendcallback(item.getDishes_price());
            if (disNo <= 0) {
                itemSub.setVisibility(View.INVISIBLE);
                tvNo.setVisibility(View.INVISIBLE);

            } else {
                itemSub.setVisibility(View.VISIBLE);
                tvNo.setVisibility(View.VISIBLE);
                tvNo.setText(disNo+"");
            }
        });
    }

    public void setImage(ImageView image, String url) {
        ImageLoader imageLoader = MyApplication.getImageLoader();
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(image,
                R.mipmap.moren_cg, R.mipmap.moren_cg);
        imageLoader.get(Model.BASEURL + url, listener);
    }

    public BigDecimal turn(double val, int i) {
        BigDecimal bd = new BigDecimal(val);
        bd = bd.setScale(i, BigDecimal.ROUND_HALF_UP);//BigDecimal.ROUND_HALF_UP 是4舍5入
        return bd;
    }


}
